package com.qf.controller;

import com.qf.entity.Health;
import com.qf.entity.Organ;
import com.qf.service.HealthService;
import com.qf.service.OrganService;
import com.qf.service.impl.HealthServiceImpl;
import com.qf.service.impl.OrganServiceImpl;
import com.qf.util.Constants;
import com.qf.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
杨威
2021/10/5
14:56
*/
@WebServlet("/health")
public class HealthController extends BaseController{
    HealthService healthService=new HealthServiceImpl();
    public String healthList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageIndex = req.getParameter("pageIndex");
        if(pageIndex==null){
            pageIndex="1";
        }
        Page page=new Page(Integer.valueOf(pageIndex));
        List<Health> healthList = healthService.showAll(page);
        req.setAttribute("page",page);
        req.setAttribute("healthList",healthList);
        return Constants.FORWARD+"/list.jsp";
    }
    public String modify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Integer status = Integer.valueOf(req.getParameter("status"));
        String deadline = req.getParameter("deadline");
        Double money = Double.valueOf(req.getParameter("money"));
        String oid = req.getParameter("oid");
        String projectname = req.getParameter("projectname");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(deadline);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(id==""){//新增
            Health health=new Health(0,projectname,status,parse,money);
            Organ organ=new Organ();
            organ.setOid(Integer.valueOf(oid));
            health.setOrgan(organ);
            int add = healthService.add(health);
            return Constants.REDIRECT+"/health?method=healthList";
        }else {//修改
            Health health=new Health(Integer.valueOf(id),projectname,status,parse,money);
            Organ organ=new Organ();
            organ.setOid(Integer.valueOf(oid));
            health.setOrgan(organ);
            int i = healthService.updateHealth(health);
            return Constants.REDIRECT+"/health?method=healthList";
        }

    }
    public String showHealth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Health health = healthService.showHealth(Integer.valueOf(id));
        OrganService organService=new OrganServiceImpl();
        List<Organ> organList = organService.showAll();
        req.setAttribute("organList",organList);
        req.setAttribute("health",health);
        return Constants.FORWARD+"/edit.jsp";
    }
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        int i = healthService.deleteHealth(Integer.valueOf(id));
        return Constants.REDIRECT+"/health?method=healthList";
    }
}
